package MyCustomTable;

import MyUtil.DataBaseUtil;
import MyUtil.JScrollUtil;
import MyUtil.MyPanel;
import MyUtil.ResultSetTableModel;

import javax.swing.*;
import java.awt.*;
import java.sql.*;

public class Member_discount_Set extends JFrame{
    //sql语句
    private String sql ="select Clevel 顾客等级,Caccount 相应折扣 from Member";
    private String sql2 = "Update Member set Caccount = ? where Clevel = ?";
    private String sql3 = "insert into Member values(?,?)";
    private String sql4 = "delete from Member where Clevel = ?";
    private JScrollPane jScrollPane;
    private JButton change_button = new JButton("修改");
    private JButton add_button = new JButton("添加");
    private JButton del_button = new JButton("删除");
    private JLabel clevel_label = new JLabel("想要修改/添加/删除的会员等级：");
    private JLabel caccount_label = new JLabel("修改/添加的额度:");
    private JTextField clevel_TextField = new JTextField(5);
    private JTextField caccount_TextField = new JTextField(5);
    private JPanel myPanel;
    private JPanel myPanel2;
    public Member_discount_Set() throws SQLException {
        super("会员额度修改");
        //基础设置
        this.setBounds(300,200,1100,600);
        //设置控件
        myPanel = new MyPanel();
        this.setContentPane(myPanel);
        myPanel2 = new MyPanel("/picture/3.jpg");
        //设置布局
        myPanel.setLayout(new BorderLayout());
        myPanel2.setLayout(new FlowLayout());
        //ScrollPane的设置
        jScrollPane = JScrollUtil.getCustomJScrollPane(DataBaseUtil.produceResultSet(sql));
        //加入控件
        myPanel.add(jScrollPane,BorderLayout.WEST);
        myPanel.add(myPanel2,BorderLayout.EAST);


        myPanel2.add(clevel_label);
        myPanel2.add(clevel_TextField);
        myPanel2.add(caccount_label);
        myPanel2.add(caccount_TextField);
        myPanel2.add(change_button);
        myPanel2.add(add_button);
        myPanel2.add(del_button);
        //button设置
        change_button.addActionListener((e)->{
            String p1 = caccount_TextField.getText();
            String p2 = clevel_TextField.getText();
            String[] p3 =  new String[2];
            p3[0] = p1;
            p3[1] = p2;
            if((p1.equals("")) ||(p2.equals(""))){
                JOptionPane.showMessageDialog(this,"有输入值为空","错误",JOptionPane.PLAIN_MESSAGE);
            }else{
                try {
                    //修改数据
                    DataBaseUtil.changeResultSet(sql2,p3);
                    //删除控件
                    myPanel.remove(jScrollPane);
                    jScrollPane = JScrollUtil.getCustomJScrollPane(DataBaseUtil.produceResultSet(sql));
                    myPanel.add(jScrollPane,BorderLayout.WEST);
                    this.setVisible(false);
                    this.setVisible(true);
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
            }
        });
        add_button.addActionListener((e)->{
            String p1 = clevel_TextField.getText();
            String p2 = caccount_TextField.getText();
            int p3 = Integer.parseInt(clevel_TextField.getText());
            Float p4 = Float.parseFloat(caccount_TextField.getText());
            Object[] p5=  new Object[2];
            p5[0] = p3;
            p5[1] = p4;
            if((p1.equals("")) ||(p2.equals(""))){
                JOptionPane.showMessageDialog(this,"有输入值为空","错误",JOptionPane.PLAIN_MESSAGE);
            }else{
                try {
                    // 更新数据
                    DataBaseUtil.changeResultSet(sql3,p5);
                    // 展示
                    myPanel.remove(jScrollPane);
                    jScrollPane = JScrollUtil.getCustomJScrollPane(DataBaseUtil.produceResultSet(sql));
                    myPanel.add(jScrollPane,BorderLayout.WEST);
                    this.setVisible(false);
                    this.setVisible(true);
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
            }
        });
        del_button.addActionListener((e)->{
            String p1 = clevel_TextField.getText();
            int p3 = Integer.parseInt(clevel_TextField.getText());
            if((p1.equals(""))){
                JOptionPane.showMessageDialog(this,"有输入值为空","错误",JOptionPane.PLAIN_MESSAGE);
            }else{
                try {
                    // 更新数据
                    DataBaseUtil.changeResultSet(sql4,p3);
                    // 展示
                    myPanel.remove(jScrollPane);
                    jScrollPane = JScrollUtil.getCustomJScrollPane(DataBaseUtil.produceResultSet(sql));
                    myPanel.add(jScrollPane,BorderLayout.WEST);
                    this.setVisible(false);
                    this.setVisible(true);
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
            }
        });
        this.setVisible(true);

    }
}
